﻿#include "AccordLaw.h"
#include "DatabaseMgmt.h"

CAccordLaw::CAccordLaw()
{

}

CAccordLaw::~CAccordLaw()
{

}

int CAccordLaw::queryByReportUid(uint32_t report_id, QVector<ViewAccordLaw>& accords)
{
	int ret = CONST_ERR_SUCCESS;

	QString strSql = QString("SELECT ACCORD_ID,REPORT_UID,BASIS_UID,STANDARD_NAME FROM VIEW_TEST_REPORT_LAW_INFO"
		" WHERE REPORT_UID=%1")
		.arg(report_id);

	QSqlQuery query;
	CDatabaseMgmt::getInstance()->execSql(strSql, query);

	if (query.lastError().type() != QSqlError::NoError)
	{
		qInfo() << query.lastError().text();
		ret = CONST_ERR_EXEC_SQL_FAILED;
	}
	else
	{
		while (query.next())
		{
			ViewAccordLaw accord;

			accord.accord_uid = query.value("ACCORD_ID").toUInt();
			accord.report_uid = query.value("REPORT_UID").toUInt();
			accord.appraisal_uid = query.value("BASIS_UID").toUInt();
			accord.appraisal_name = query.value("STANDARD_NAME").toString();

			accords.append(accord);
		}
	}

	return ret;
}

int CAccordLaw::queryByReportUid(uint32_t report_id, QVector<AccordLaw>& accords)
{
	int ret = CONST_ERR_SUCCESS;

	QString strSql = QString("SELECT UID,TEST_REPORT_UID,APPRAISAL_BASIS_UID FROM TB_ACCORD_LAW"
		" WHERE TEST_REPORT_UID=%1")
		.arg(report_id);

	QSqlQuery query;
	CDatabaseMgmt::getInstance()->execSql(strSql, query);

	if (query.lastError().type() != QSqlError::NoError)
	{
		qInfo() << query.lastError().text();
		ret = CONST_ERR_EXEC_SQL_FAILED;
	}
	else
	{
		while (query.next())
		{
			AccordLaw accord;

			accord.uid = query.value("UID").toUInt();
			accord.report_uid = query.value("TEST_REPORT_UID").toUInt();
			accord.appraisal_uid = query.value("APPRAISAL_BASIS_UID").toUInt();;

			accords.append(accord);
		}
	}

	return ret;
}

int CAccordLaw::modifyByUid(AccordLaw accord)
{
	int ret = CONST_ERR_SUCCESS;

	QString strSql = QString("UPDATE TB_ACCORD_LAW SET TEST_REPORT_UID=%1, APPRAISAL_BASIS_UID=%2,"
		" WHERE UID=%3")
		.arg(accord.report_uid)
		.arg(accord.appraisal_uid)
		.arg(accord.uid);

	QSqlQuery query;
	CDatabaseMgmt::getInstance()->execSql(strSql, query);

	if (query.lastError().type() != QSqlError::NoError)
	{
		ret = CONST_ERR_EXEC_SQL_FAILED;
		qInfo() << query.lastError().text();
	}

	return ret;
}

int CAccordLaw::insertRow(AccordLaw accord, uint32_t& uid)
{
	int ret = CONST_ERR_SUCCESS;

	QString strSql = QString("INSERT INTO TB_ACCORD_LAW (TEST_REPORT_UID, APPRAISAL_BASIS_UID)"
		" VALUES (%1,%2);")
		.arg(accord.report_uid)
		.arg(accord.appraisal_uid);

	QSqlQuery query;
	CDatabaseMgmt::getInstance()->execSql(strSql, query);

	if (query.lastError().type() != QSqlError::NoError)
	{
		qInfo() << query.lastError().text();
		ret = CONST_ERR_EXEC_SQL_FAILED;
	}
	else
	{
		strSql = " SELECT LAST_INSERT_ROWID() FROM [TB_ACCORD_LAW]; ";
		CDatabaseMgmt::getInstance()->execSql(strSql, query);

		if (query.lastError().type() != QSqlError::NoError)
		{
			ret = CONST_ERR_EXEC_SQL_FAILED;
			qInfo() << query.lastError().text();
		}
		else
		{
			if (query.next())
			{
				uid = query.value(0).toUInt();
			}
			else
			{
				uid = 0;
				qInfo() << query.lastError().text();
			}
		}
	}
	return ret;
}

int CAccordLaw::insertRow(AccordLaw accord)
{
	int ret = CONST_ERR_SUCCESS;

	QString strSql = QString("INSERT INTO TB_ACCORD_LAW (TEST_REPORT_UID, APPRAISAL_BASIS_UID)"
		" VALUES (%1,%2);")
		.arg(accord.report_uid)
		.arg(accord.appraisal_uid);

	QSqlQuery query;
	CDatabaseMgmt::getInstance()->execSql(strSql, query);

	if (query.lastError().type() != QSqlError::NoError)
	{
		qInfo() << query.lastError().text();
		ret = CONST_ERR_EXEC_SQL_FAILED;
	}
	return ret;
}

int CAccordLaw::removeRow(uint32_t uid)
{
	int ret = CONST_ERR_SUCCESS;

	QString strSql = QString("DELETE FROM TB_ACCORD_LAW WHERE UID=%1")
		.arg(uid);

	QSqlQuery query;
	CDatabaseMgmt::getInstance()->execSql(strSql, query);

	if (query.lastError().type() != QSqlError::NoError)
	{
		ret = CONST_ERR_EXEC_SQL_FAILED;
		qInfo() << query.lastError().text();
	}

	return ret;
}

int CAccordLaw::removeByReportUid(uint32_t report_id)
{
	int ret = CONST_ERR_SUCCESS;

	QString strSql = QString("DELETE FROM TB_ACCORD_LAW WHERE TEST_REPORT_UID=%1")
		.arg(report_id);

	QSqlQuery query;
	CDatabaseMgmt::getInstance()->execSql(strSql, query);

	if (query.lastError().type() != QSqlError::NoError)
	{
		ret = CONST_ERR_EXEC_SQL_FAILED;
		qInfo() << query.lastError().text();
	}

	return ret;
}
